package org.qfbl.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 使用channel和bytebuffer进行文件拷贝
 *
 */
public class NIOFileChannel03 {
    public static void main(String[] args) {
        try {



            File file = new File("E:\\JavaCode\\netty_project\\file\\01file.txt");
            FileInputStream inputStream = new FileInputStream(file);
            FileChannel channel = inputStream.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
            int read = channel.read(byteBuffer);
            inputStream.close();
            byteBuffer.flip();
            FileOutputStream fileOutputStream = new FileOutputStream("E:\\JavaCode\\netty_project\\file\\02file.txt");
            FileChannel writeChannel = fileOutputStream.getChannel();
            int write = writeChannel.write(byteBuffer);
            fileOutputStream.close();


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
